<template>
  <div>
    <van-nav-bar
      :title="`${$route.query.key} 的搜索结果`"
      fixed
      placeholder
      left-arrow
      @click-left="$router.back()"
    />

    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <van-cell
        v-for="item in list"
        :key="item.art_id"
        @click="$router.push(`/article?id=${item.art_id}`)"
      >
        <div>{{ item.title }}</div>
        <!-- 图片部分 -->
        <van-grid :border="false" :column-num="item.cover.type">
          <van-grid-item v-for="(pic, index) in item.cover.images" :key="index">
            <van-image :src="pic" />
          </van-grid-item>
        </van-grid>
      </van-cell>
    </van-list>
  </div>
</template>

<script>
import { resultAPI } from '@/api/search'
export default {
  data() {
    return {
      loading: false,
      finished: false,
      list: [],
      page: 1
    }
  },
  methods: {
    async onLoad() {
      const res = await resultAPI({
        page: this.page,
        q: this.$route.query.key
      })
      this.page++

      this.loading = false

      this.list.push(...res.data.results)

      if (this.list.length <= res.data.total_count) {
        this.finished = true
      }
    }
  }
}
</script>

<style></style>
